package com.wh.hotel.controller;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.wh.common.annotation.ResultLog;
import com.wh.common.enums.MethodTypeEnum;
import com.wh.common.vo.Result;
import com.wh.hotel.enums.strategy.PriceStrategyTypeEnum;
import com.wh.hotel.param.PriceStrategyQueryParam;
import com.wh.hotel.po.PriceHitStrategyPo;
import com.wh.hotel.po.SuitHotelRoomPo;
import com.wh.hotel.service.HotelPriceStrategyService;
import com.wh.hotel.util.DateWhUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 价格日历
 *
 * @author luweije
 */
@RequestMapping("/hotel/price/calendar")
@RestController
@Slf4j
public class HotelPriceCalendarController {

    @Resource
    private HotelPriceStrategyService hotelPriceStrategyService;

    @PostMapping("/queryByDate")
    @ResultLog(name = "HotelBookRuleController.queryByDate", methodType = MethodTypeEnum.HTTP_UP)
    public Result<List<PriceHitStrategyPo>> queryByDate(@RequestBody PriceStrategyQueryParam param) {
        Preconditions.checkNotNull(param.getHotelId(), "酒店Id不能空");
        Preconditions.checkNotNull(param.getStartDt(), "开始日期不能空");
        Preconditions.checkNotNull(param.getEndDt(), "结束日期不能空");
        SuitHotelRoomPo suitHotelRoomPo = new SuitHotelRoomPo();
        suitHotelRoomPo.setHotelId(param.getHotelId());
        if (param.getRoomTypeId() != null) {
            suitHotelRoomPo.setRoomTypeIdList(Lists.newArrayList(param.getRoomTypeId()));
        }
        List<Date> dateList = DateWhUtil.createDateByDay(param.getStartDt(), param.getEndDt());
        List<PriceHitStrategyPo> priceHitStrategyPoList = hotelPriceStrategyService.hitPriceStrategy(suitHotelRoomPo, dateList, PriceStrategyTypeEnum.BASE_PRICE);
        hotelPriceStrategyService.updatePriceCalendar(priceHitStrategyPoList);
        return Result.successResponse(priceHitStrategyPoList);
    }
}
